<?php

namespace App\Admin\Controllers;

use App\Models\Storeys;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;

class StoreysController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '楼层';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Storeys());

        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $row->column('number', $number+1);
        });
        $grid->column('name', __('楼层名称'));
        $grid->column('sort', __('排序'))->sortable();
        $grid->column('created_at', __('Created at'))->sortable();
        $grid->column('updated_at', __('Updated at'))->sortable();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('name', '楼层名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Storeys::findOrFail($id));

        $show->field('name', __('楼层名称'));
        $show->field('sort', __('排序'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Storeys());

        $form->hidden('store_id', __('门店id'));
        $form->text('name', __('Name'))->required();
        $form->text('sort', __('Sort'));

        $form->saving(function (Form $form) {
            $form->store_id = Admin::user()->store_id;
        });

        return $form;
    }
}
